<template>
    <div class="login-container">
        <div class="login-left-part"></div>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
            label-position="top">
            <div class="title-container">
                <h3 class="title"><span>首次登录修改密码</span></h3>
                <div style="color: #7881ad;margin-bottom: 40px;">Sign in and start managing your account.</div>
            </div>
            <div class="lab">账 号</div>
            <el-form-item prop="username">
                <span class="svg-container">
                    <svg-icon icon-class="user" />
                </span>
                <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text"
                    tabindex="1" autocomplete="on" />
            </el-form-item>
            <div class="lab">原始密码</div>
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input :key="pwd" ref="pwd" v-model="loginForm.password" :type="pwd" placeholder="Password"
                    name="password" tabindex="2" autocomplete="on" @keyup.enter.native="handleLogin" />
                <span class="show-pwd" @click="showPwd('pwd')">
                    <svg-icon :icon-class="pwd === 'password' ? 'eye' : 'eye-open'" />
                </span>
            </el-form-item>
            <div class="lab">新密码</div>
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input :key="pwd" ref="pwd" v-model="loginForm.password" :type="pwd" placeholder="Password"
                    name="password" tabindex="2" autocomplete="on" @keyup.enter.native="handleLogin" />
                <span class="show-pwd" @click="showPwd('pwd')">
                    <svg-icon :icon-class="pwd === 'password' ? 'eye' : 'eye-open'" />
                </span>
            </el-form-item>
            <div class="lab">密码强度</div>
            <el-form-item prop="userCode">
                <el-progress :percentage="50"></el-progress>
            </el-form-item>
            <div class="lab">确认新密码</div>
            <el-form-item prop="password">
                <span class="svg-container">
                    <svg-icon icon-class="password" />
                </span>
                <el-input :key="pwd" ref="pwd" v-model="loginForm.password" :type="pwd" placeholder="Password"
                    name="password" tabindex="2" autocomplete="on" @keyup.enter.native="handleLogin" />
                <span class="show-pwd" @click="showPwd('pwd')">
                    <svg-icon :icon-class="pwd === 'password' ? 'eye' : 'eye-open'" />
                </span>
            </el-form-item>
            <el-button class="lbtn" :loading="loading" type="primary" @click.native.prevent="handleLogin('passForm')">完 成</el-button>
        </el-form>
    </div>
</template>

<script>
    import {
        validUsername
    } from '@/utils/validate';
    import {
        login
    } from '@/api/user.js'

    export default {
        name: 'Login',
        data() {
            const validateUsername = (rule, value, callback) => {
                if (!validUsername(value)) {
                    callback(new Error('Please enter the correct user name'))
                } else {
                    callback()
                }
            }
            const validatePassword = (rule, value, callback) => {
                if (value && value.length < 5) {
                    callback(new Error('密码不少于6位'))
                } else {
                    callback()
                }
            }
            return {
                pwd: 'password',
                pwd1: 'password',
                pwd2: 'password',
                capsTooltip: false,
                loading: false,
                redirect: undefined,
                isLogin: 'loginForm',
                /* 倒计时 */
                count: '',
                timer: null,
                showTime: false,
                /* 登录 系列*/
                loginForm: {
                    username: 'admin',
                    password: 'admin',
                    vcToken: '',
                    vc: '',
                    url: ''
                },
                loginRules: {
                    username: [{
                        required: true,
                        trigger: 'blur',
                        validator: validateUsername
                    }],
                    password: [{
                        required: true,
                        trigger: 'blur',
                        validator: validatePassword
                    }]
                },
            }
        },
        created() {
            // window.addEventListener('storage', this.afterQRScan)
        },
        mounted() {},
        destroyed() {
            // window.removeEventListener('storage', this.afterQRScan)
        },
        methods: {
            /* 获取图形验证码*/
            getImgCode() {
                this.$axios.post(process.env.VUE_APP_BASE_API+'/b/privs/vc', {
                    responseType: 'blob',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                }).then(data => {
                    this.loginForm.vcToken = data.headers.vctoken;
                    let blobUrl = window.URL.createObjectURL(data.data);
                    this.loginForm.url = blobUrl;
                }).catch(() => {
                    this.$message.error("验证码获取超时!")
                });
            },
            checkCapslock(e) {
                const {
                    key
                } = e
                this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
            },
            showPwd(e) {
                if (e == 'pwd') {
                    if (this.pwd == 'password') {
                        this.pwd = ''
                    } else {
                        this.pwd = 'password'
                    }
                } else if (e == 'pwd1') {
                    if (this.pwd1 == 'password') {
                        this.pwd1 = ''
                    } else {
                        this.pwd1 = 'password'
                    }
                } else {
                    if (this.pwd2 == 'password') {
                        this.pwd2 = ''
                    } else {
                        this.pwd2 = 'password'
                    }
                }

                this.$nextTick(() => {
                    this.$refs[e].focus()
                })
            },
            rePwd(e) {
                if (e) {
                    let form = this.loginForm;
                    localStorage.setItem('encryptionMd5', JSON.stringify(form));
                } else {
                    localStorage.removeItem('encryptionMd5');
                }
            },
            /* 获取手机验证码 */
            getCode() {
                const TIME_COUNT = 60;
                if (!this.timer) {
                    this.count = TIME_COUNT;
                    this.showTime = true;
                    this.timer = setInterval(() => {
                        if (this.count > 0 && this.count <= TIME_COUNT) {
                            this.count--;
                        } else {
                            this.showTime = false;
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }, 1000);
                }
            },
            handleLogin(e) {
                this.loading = true;
                if (e == 'passForm') {
                    this.$refs.passForm.validate(valid => {
                        if (valid) {
                            this.loading = false;
                            this.$notify({
                                title: '成功',
                                message: '密码重置成功!请重新登录',
                                type: 'success'
                            });
                            this.isLogin = 'loginForm'
                        } else {
                            let _this = this;
                            setTimeout(() => {
                                _this.loading = false
                            }, 1000)
                            console.log('error submit!!');
                            return false
                        }
                    })
                } else if (e == 'loginForm') {
                    this.$refs.loginForm.validate(valid => {
                        if (valid) {
                            this.loading = true;
                            this.$store.dispatch('user/login', this.loginForm);
                            /* 暂时设置角色缓存 */
                            sessionStorage.setItem('roles', this.loginForm.username);
                            // let form = this.loginForm;
                            // login({
                            //     username: form.username,
                            //     password: form.password,
                            //     vc: form.vc,
                            //     vcToken: form.vcToken
                            // }).then(data => {
                            //     console.log(data);
                            //     this.$router.push({
                            //         path: this.redirect || '/manager/index',
                            //     })
                            //     this.loading = false
                            // })
                            this.loading = false


                            this.$router.push({
                                path: this.redirect || '/manager/index',
                            })
                            this.$message({
                                message: '登录成功!',
                                type: 'success',
                                duration: 1000,
                            });
                        } else {
                            console.log('error submit!!')
                            return false
                        }
                    })
                } else {
                    this.$refs.resetForm.validate(valid => {
                        if (valid) {
                            this.loading = false;
                            this.isLogin = 'passForm';
                        } else {
                            let _this = this;
                            setTimeout(() => {
                                _this.loading = false
                            }, 1000)
                            console.log('error submit!!');
                            return false
                        }
                    })
                }


            },
        }
    }
</script>

<style lang="scss">
    /* 修复input 背景不协调 和光标变色 */
    /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

    $bg:#283443;
    $light_gray:#fff;
    $cursor: #7881ad;

    @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
        .login-container .el-input input {
            color: #7881ad;
        }
    }

    .iconClass {
        background-color: #f0f9eb !important;
        border-color: #409EFF !important;
    }

    /* reset element-ui css */
    .login-container {
        .el-input {
            display: inline-block;
            height: 47px;
            width: 85%;

            input {
                background: transparent;
                border: 0px;
                -webkit-appearance: none;
                border-radius: 0px;
                padding: 12px 5px 12px 15px;
                color: #7881ad;
                height: 47px;
                caret-color: $cursor;

                &:-webkit-autofill {
                    box-shadow: 0 0 0px 1000px $bg inset !important;
                    -webkit-text-fill-color: $cursor !important;
                }
            }
        }

        .el-form-item {
            border: 1px solid #eaeaea;
            border-radius: 5px;
            color: #454545;
        }
    }
</style>

<style lang="scss" scoped>
    $bg:#f8f8f8;
    $dark_gray:#889aa4;
    $light_gray:#3a3f63;

    //flex 布局和 子元素 对其方式
    @mixin fj($type: space-between) {
        display: flex;
        justify-content: $type;
    }

    //宽高
    @mixin wh($width, $height) {
        width: $width;
        height: $height;
    }

    .login-container {
        min-height: 100%;
        width: 100%;
        background-color: $bg;
        @include fj(center);
        align-items: center;
        overflow: hidden;

        /* 左侧海报 */
        /* .login-left-part {
            @include wh(480px, 900px);
            background: url(img/login.png) no-repeat;
        } */

        .login-form {
            position: relative;
            @include wh(920px, 900px);
            max-width: 100%;
            padding: 150px 170px 0 170px;
            background-color: #FFFFFF;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
            overflow: hidden;

            .remember_pwd {
                line-height: 25px;
                @include fj(space-between);
            }

            .lab {
                color: #616582;
                margin-bottom: 5px;
            }

            .lbtn {
                width: 40%;
                height: 50px;
                margin: 25px 0;
                border-radius: 30px;
                background: linear-gradient(to bottom, #4186ff, #4b61ff);
            }

            .cbtn {
                height: 52px;
                width: 32%;
                float: right;
                background: linear-gradient(to bottom, #4186ff, #4b61ff);
            }

            .ltxt {
                color: #7881ad;
                font-size: 16px;
                line-height: 30px;
                font-weight: 550;

                .txt_span {
                    cursor: pointer;
                    font-size: 17px;
                    color: #3A3F63;
                    margin-left: 5px;
                }
            }

            .nopwd {
                font-weight: 600;
                color: #616582;
                text-align: center;
                cursor: pointer;
            }
        }

        .svg-container {
            padding: 6px 5px 6px 15px;
            color: $dark_gray;
            vertical-align: middle;
            width: 30px;
            display: inline-block;
        }

        .title-container {
            position: relative;

            .title {
                font-size: 26px;
                color: $light_gray;
                margin: 0px auto 10px auto;
                text-align: left;
                font-weight: bold;
            }
        }

        .show-pwd {
            position: absolute;
            right: 10px;
            top: 7px;
            font-size: 16px;
            color: $dark_gray;
            cursor: pointer;
            user-select: none;
        }


        @media only screen and (max-width: 900px) {
            .login-left-part {
                display: none;
            }

            .login-form {
                padding: 8%;
            }
        }

        @media only screen and (min-width: 0px) and (max-width: 600px) {
            .lbtn {
                width: 100% !important;
            }
        }
    }
</style>
